package com.gxwz.medical.domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxwz.medical.database.DbUtil;

public class Reimburse {
	
	/**
	 * 身份证号
	 */
	private String cardID;	
	/**
	 * 年度
	 */
    private String runyear;
	/**
	 * 姓名
	 */
	private String persname;
	/**
	 * 花费金额
	 */
	private String pay;
	/**
	 * 报销金额
	 */
	private String paysum;
	
    private String status;
	
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Reimburse (){
		
	}

	public Reimburse(String cardID, String runyear, String persname,
			String pay, String paysum, String status) {
		super();
		this.cardID = cardID;
		this.runyear = runyear;
		this.persname = persname;
		this.pay = pay;
		this.paysum = paysum;
		this.status = status;
	}
	public Reimburse(String cardID)throws SQLException{
		try{
		this.cardID=cardID;
		loadDB(cardID);
	    }catch(SQLException e){
	    	throw e;
	    }
		
	}
	
	private void loadDB(String cardID)throws SQLException{
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement("select * from t_reimburse where cardID=?");
			int index=1;
			pstmt.setString(index, this.cardID);
			rs=pstmt.executeQuery();
			if(rs.next()){
				this.cardID=rs.getString("cardID");
				this.runyear =rs.getString("runyear");
				this.persname =rs.getString("persname");
				this.pay =rs.getString("pay");
				this.paysum =rs.getString("paysum");
				this.status =rs.getString("status");
			}
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	}
	

	
	/**
	 * 修改慢病报销信息
	 * @throws Exception
	 */
	
	public void edit()throws SQLException{
		Connection conn =DbUtil.getConn();
		try{
			 conn.setAutoCommit(false);
			 this.updateToDB(conn);
			 conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	private void updateToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造update SQL语句字符串变量sql
			String  sql="update t_reimburse set runyear=?,persname=?,pay=?,paysum=?,status=? where cardID=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.runyear);
			 pstmt.setString(index++, this.persname);
			 pstmt.setString(index++, this.pay);
			 pstmt.setString(index++, this.paysum);
			 pstmt.setString(index++, this.status);
			 pstmt.setString(index++, this.cardID);
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
		}
		
	}
	
	
	/**
	 * 添加慢病证信息
	 * @throws Exception
	 */
	
	public void add()throws Exception{
		Connection conn=null;
		try{
			//1:对象属性赋值,无
    	    conn=DbUtil.getConn();
    	   //2；调用saveToDB方法保存到数据库
    	    saveToDB(conn);
		}catch(SQLException e){
	    	   throw e;
	       }finally{
	    	   DbUtil.close(conn);
	       }
		
	}
	/**
	 * 持久化数据
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{

		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("INSERT INTO t_reimburse (cardID,runyear,persname,pay,paysum,status)");
			  			   sqlBuff.append("VALUES (?, ?, ?, ?, ?,?)");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  int index=1;
			  pstmt.setString(index++, this.cardID);
			  pstmt.setString(index++, this.runyear);
			  pstmt.setString(index++, this.persname);
			  pstmt.setString(index++, this.pay);
			  pstmt.setString(index++, this.paysum);
			  pstmt.setString(index++, this.status);	
			  pstmt.addBatch();
			  int count =pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  e.printStackTrace();
			  throw e;
		  }finally{
			  DbUtil.close(pstmt);
		  } 
}
	public void del(String cardID)throws SQLException{

		Connection conn=null;
       try{
    	   //1:对象属性赋值
    	    this.cardID =cardID;
    	    conn=DbUtil.getConn();
    	   //2；调用delFromDB方法
    	    delFromDB(conn);
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DbUtil.close(conn);
       }
		
	}
	private void delFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造插入SQL语句字符串变量sql
			String  sql="delete from t_reimburse where cardID=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.cardID);
		
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
			
		}		
	}
	public String getRunyear() {
		return runyear;
	}
	public void setRunyear(String runyear) {
		this.runyear = runyear;
	}

	public String getPersname() {
		return persname;
	}
	public void setPersname(String persname) {
		this.persname = persname;
	}
	public String getCardID() {
		return cardID;
	}
	public void setCardID(String cardID) {
		this.cardID = cardID;
	}
	public String getPay() {
		return pay;
	}
	public void setPay(String pay) {
		this.pay = pay;
	}
	public String getPaysum() {
		return paysum;
	}
	public void setPaysum(String paysum) {
		this.paysum = paysum;
	}

}